package cn.edu.neusoft.demo.hospital2.Controller;

import cn.edu.neusoft.demo.hospital2.Service.DrugService;
import cn.edu.neusoft.demo.hospital2.pojo.Drug;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/drug")
public class DrugController {
    @Autowired
    private DrugService drugService;

    @GetMapping("/all")
    public Map<String, Object> getAllDrugs() {
        Map<String, Object> result = new HashMap<>();
        List<Drug> list = drugService.getAllDrugs();
        result.put("code", 200);
        result.put("data", list);
        result.put("message", "查询成功");
        return result;
    }

    @GetMapping("/{drugId}")
    public Map<String, Object> getDrugById(@PathVariable Integer drugId) {
        Map<String, Object> result = new HashMap<>();
        Drug drug = drugService.getDrugById(drugId);
        if (drug != null) {
            result.put("code", 200);
            result.put("data", drug);
            result.put("message", "查询成功");
        } else {
            result.put("code", 404);
            result.put("message", "未找到该药品记录");
        }
        return result;
    }

    @PostMapping("/add")
    public Map<String, Object> addDrug(@RequestBody Drug drug) {
        Map<String, Object> result = new HashMap<>();
        int res = drugService.insertDrug(drug);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "新增成功");
        } else {
            result.put("code", 500);
            result.put("message", "新增失败");
        }
        return result;
    }

    @PutMapping("/update")
    public Map<String, Object> updateDrug(@RequestBody Drug drug) {
        Map<String, Object> result = new HashMap<>();
        int res = drugService.updateDrug(drug);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "更新成功");
        } else {
            result.put("code", 500);
            result.put("message", "更新失败");
        }
        return result;
    }

    @DeleteMapping("/delete/{drugId}")
    public Map<String, Object> deleteDrug(@PathVariable Integer drugId) {
        Map<String, Object> result = new HashMap<>();
        int res = drugService.deleteDrug(drugId);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }

    @PostMapping("/copy/{drugId}")
    public Map<String, Object> copyDrug(@PathVariable Integer drugId) {
        Map<String, Object> result = new HashMap<>();
        int res = drugService.copyDrug(drugId);
        if (res > 0) {
            result.put("code", 200);
            result.put("message", "复制成功");
        } else {
            result.put("code", 500);
            result.put("message", "复制失败");
        }
        return result;
    }
}